<template>
  <div class="home">
    <el-container class="layout-container">
      <div class="h100">
        <div class="navbar-log">
          <img src="https://s.weituibao.com/1582958061265/mlogo.png" alt="">
          <span>vue3 admin</span>
        </div>
        <el-aside width="200px" class="layout-aside">
          <el-scrollbar>
            <layout-nav></layout-nav>
          </el-scrollbar>
        </el-aside>
      </div>

      <el-container>
        <el-header style="text-align: right; font-size: 12px">
          <div class="toolbar">
            <el-dropdown>
              <el-icon style="margin-right: 8px; margin-top: 1px">
                <setting />
              </el-icon>
              <template #dropdown>
                <el-dropdown-menu>
                    <div class="nickname">
                    <p>登录名：{{ userInfo && userInfo.loginUserName || '' }}</p>
                    <p>昵称：{{ userInfo && userInfo.nickName || '' }}</p>
                    <el-tag size="small" effect="dark" class="logout" @click="logout">退出</el-tag>
                  </div>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
            <span>昵称：{{ userInfo && userInfo.nickName || '' }}</span>
          </div>
        </el-header>

        <el-main>
            <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
import { onMounted, reactive, toRefs } from 'vue'
import { useRouter,useRoute } from 'vue-router'
import api from '../../http'
import { localRemove } from '../../utils'
import LayoutNav from "./compoents/LayoutNav.vue";
import { ElMessage } from 'element-plus';
export default {
  name: 'myHome',
  components:{
    LayoutNav
  },
  setup() {
    const router = useRouter(),
    route =  useRoute(),
    state = reactive({
      name: 'myHome',
      userInfo: null,
      hasBack: false
    })
    onMounted(() => {
  
      const pathname = route.fullPath.split('/')[1] || ''
      if (![''].includes(pathname)) {
        getUserInfo()
      }
    })
    const getUserInfo = async () => {
      const {resultCode,data}= await api.login.adminUserProfile();
      if(resultCode!==200){
        //跳转到登录页
        router.push("/");
        return;
      }
      state.userInfo = data;
    }
    const logout = async () => {
      let {resultCode,data} = await api.login.logOut();
      if(resultCode!==200){
        ElMessage.error("退出失败~");
        return
      }
      console.log(data);
      localRemove('token')
      ElMessage.success("退出成功~");
      router.push("/");
    }
    const back = () => {
      router.back()
    }
    return{
        ...toRefs(state),
        logout,
        back,
      LayoutNav,
    }
  }
}
 
</script>


<style lang="less" scoped>
  .home {
    width: 100%;
    height: 100%;

    .layout-container {
      width: 100%;
      height: 100%;

      .h100 {
        height: 100%;
        background-color: #222832;

        .navbar-log {
          width: 100%;
          height: 60px;
          display: flex;
          align-items: center;
          justify-content: center;

          img {
            width: 50px;
            height: 50px;
            margin-right: 10px;
          }

          span {
            font-size: 20px;
            color: #fff;
          }
        }

        .layout-aside {
          background: #222832;
          box-shadow: 2px 0 6px #00152903;
          height: inherit;
          position: relative;
          z-index: 1;
          display: flex;
          flex-direction: column;
          overflow-x: hidden !important;

          // height: 91.7vh;
          /deep/ .el-scrollbar__view {
            height: 91.7vh;
          }
        }
      }
    }

    .el-header {
      position: relative;
      background-color: var(--el-color-primary-light-7);
      color: var(--el-text-color-primary);
    }

    .el-main {
      padding: 10px;
      height: 91.7vh;
    }

    .toolbar {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      height: 100%;
      right: 20px;
    }
  }
</style>